package com.qiaofang.collectmanage.stub.feign.client.impl;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.collectmanage.stub.bean.*;
import com.qiaofang.collectmanage.stub.facade.CollectRuleFacade;
import com.qiaofang.collectmanage.stub.feign.client.CollectRuleClient;
import com.qiaofang.collectmanage.stub.request.*;
import com.qiaofang.collectmanage.stub.response.*;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.response.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author li.yi
 * @date 2018年6月29日
 */
@Service("collectRuleClient")
public class CollectRuleClientImpl implements CollectRuleClient{

	@Autowired
	private CollectRuleFacade collectRuleFacade;

	@Override
	public CollectRuleDTO getCollectRuleById(Integer id) {
		GetCollectRuleByUuidRequest request = new GetCollectRuleByUuidRequest();
		request.setId(id);
		GetCollectRuleByUuidResponse response = collectRuleFacade.getCollectRuleById(request);
		if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			return response.getData();
		} else {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

	@Override
	public CollectRuleDTO getCollectRule(GetCollectRuleDTO getCollectRuleDTO) {
		GetCollectRuleRequest request = new GetCollectRuleRequest();
		request.setGetCollectRuleDTO(getCollectRuleDTO);
		GetCollectRuleResponse response = collectRuleFacade.getCollectRule(request);
		if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			return response.getData();
		} else {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

	@Override
	public void saveCollectRule(SaveCollectRuleDTO saveCollectRuleDTO) {
		SaveCollectRuleRequest request = new SaveCollectRuleRequest();
		request.setSaveCollectRuleDTO(saveCollectRuleDTO);
		CommonResponse response = collectRuleFacade.saveCollectRule(request);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

	@Override
	public void updateCollectRule(UpdateCollectRuleDTO updateCollectRuleDTO) {
		UpdateCollectRuleRequest request = new UpdateCollectRuleRequest();
		request.setUpdateCollectRuleDTO(updateCollectRuleDTO);
		CommonResponse response = collectRuleFacade.updateCollectRule(request);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

	@Override
	public void removeCollectRule(RemoveCollectRuleDTO removeCollectRuleDTO) {
		RemoveCollectRuleRequest request = new RemoveCollectRuleRequest();
		request.setRemoveCollectRuleDTO(removeCollectRuleDTO);
		CommonResponse response = collectRuleFacade.removeCollectRule(request);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

	@Override
	public List<CollectRuleDTO> listCollectRule(ListCollectRuleDTO collectRuleDTO) {
		ListCollectRuleRequest request = new ListCollectRuleRequest();
		request.setCollectRuleDTO(collectRuleDTO);
		ListCollectRuleResponse response = collectRuleFacade.listCollectRule(request);
		if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			return response.getData();
		} else {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

 	@Override
	public SearchCollectRulePageDTO searchCollectRulePage(PageDTO pageDTO,SearchCollectRulePostDTO searchCollectRulePostDTO) {
		SearchCollectRulePageRequest request = new SearchCollectRulePageRequest();
		request.setPageDTO(pageDTO);
		request.setSearchCollectRulePostDTO(searchCollectRulePostDTO);
		SearchCollectRulePageResponse response = collectRuleFacade.searchCollectRulePage(request);
		if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			return response.getData();
		} else {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}


	@Override
	public void commonCityExtend( List<CommonCityExtendDTO> data, String city,String collectType,String collectHouseType, String belonger,String source) {
		CommonCityExtendRequest request = new CommonCityExtendRequest();
		request.setData(data);
		request.setCity(city);
		request.setCollectType(collectType);
		request.setCollectHouseType(collectHouseType);
		request.setBelonger(belonger);
		request.setSource(source);
		CommonResponse response = collectRuleFacade.commonCityExtend(request);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}




	@Override
	public List<String> getCitys() {
		GetCitysResponse response = collectRuleFacade.getCitys();
		if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			return response.getData();
		} else {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}

	@Override
	public  List<String> getSources() {
		GetSourcesResponse response = collectRuleFacade.getSources();
		if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			return response.getData();
		} else {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
	}


}
